//
//  MarriagePlanCreateTaskPopView.m
//  BaseProject
//
//  Created by mac on 2020/11/21.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "MarriagePlanCreateTaskPopView.h"
#import <BRPickerView.h>
#import "MarriagePlanModel.h"
@interface MarriagePlanCreateTaskPopView ()<YYTextViewDelegate>
@property(nonatomic,strong)UILabel *titleLab;
@property(nonatomic,strong)UIButton *leftButton;
@property(nonatomic,strong)UIButton *rightButton;
@property(nonatomic,strong)UILabel *timeLab;
@property(nonatomic,strong)UIControl *timeControl;
@property(nonatomic,strong)UILabel *timeContentLab;
@property(nonatomic,strong)UIImageView *timeIV;
@property(nonatomic,strong)UILabel *desLab;
@property(nonatomic,strong)YYTextView *textView;
//时间选择器
@property(nonatomic,strong)BRDatePickerView *timePickerView;
//计数label
@property(nonatomic,strong)UILabel *numberLab;
@property(nonatomic,assign)NSInteger maxNumer;
@end

@implementation MarriagePlanCreateTaskPopView
- (instancetype)initWithFrame:(CGRect)frame popStyle:(ZJAnimationPopStyle)popStyle dismissStyle:(ZJAnimationDismissStyle)dismissStyle parentView:(UIView *)parentView
{
    if (self = [super initWithFrame:frame popStyle:popStyle dismissStyle:dismissStyle parentView:parentView]) {
        [self createProperty];
        [self createUI];
    }
    return self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    NSLog(@"MarriagePlanCreateTaskPopView ---dealloc");
}
- (void)createProperty
{
    self.layer.cornerRadius = 10*ADAPTER_WIDTH;
    self.layer.masksToBounds = YES;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    self.maxNumer = 30;
}
- (void)createUI
{
    self.titleLab.text = @"我的任务";
    [self leftButton];
    [self rightButton];
    self.timeLab.text = @"时间：";
    [self timeControl];
    [self timeIV];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    self.timeContentLab.text = @"";
    self.desLab.text = @"任务：";
    [self textView];
    WK(weakSelf)
    [self.popView setClickContentViewComplete:^{
        if (!weakSelf.popView.isClickBGDismiss) {
            [weakSelf.textView resignFirstResponder];
        }
    }];
    self.numberLab.text = [NSString stringWithFormat:@"%d/%ld",0,self.maxNumer];
}
- (void)setListModel:(MarriagePlanDataListModel *)listModel
{
    _listModel = listModel;
    self.textView.text = listModel.plansContent;
    self.timeContentLab.text = listModel.overTime;
}
#pragma mark - click
- (void)clickItemButton:(UIButton *)sender
{
    switch (sender.tag) {
        case 0:
        {
            [self.popView dismiss];
        }
            break;
        case 1:
        {
            if (self.textView.text.length <= 0) {
                [SVProgressHUD showErrorWithStatus:@"请输入任务描述～"];
                [SVProgressHUD dismissWithDelay:1];
                return;
            }
            NSMutableDictionary *dic = @{}.mutableCopy;
            [dic setValue:self.textView.text forKey:@"plansContent"];
            [dic setValue:@"1" forKey:@"type"];
            [dic setValue:@"1" forKey:@"status"];
            [dic setValue:@"0" forKey:@"plansTime"];
            if (self.timeContentLab.text.length > 0) {
                [dic setValue:self.timeContentLab.text forKey:@"overTime"];
            }
            [dic setValue:[TTUserModel sharedObject].username forKey:@"userId"];
            
            NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/userMarryPlansOver/insertUserMarryPlansOver"];
            if (self.listModel != nil) {
                [dic setValue:self.listModel.umpoId forKey:@"umpoId"];
                url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,@"/userMarryPlansOver/updateUserMarryPlansOver"];
            }
            [SVProgressHUD showWithStatus:@"正在加载"];
            NSLog(@"%@",dic);
            WK(weakSelf)
            [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
                NSLog(@"%@",data);
                BaseResponse *responModel = [BaseResponse modelWithJSON:data];
                if (responModel.success) {
                    [SVProgressHUD dismiss];
                    
                    
                    if (weakSelf.listModel == nil) {
                        !weakSelf.refreshBlock ?: weakSelf.refreshBlock ();
                    } else {
                        weakSelf.listModel.overTime = weakSelf.timeContentLab.text;
                        weakSelf.listModel.plansContent = weakSelf.textView.text;
                        !weakSelf.updateBlock ?: weakSelf.updateBlock (weakSelf.listModel);
                    }
                    
                    [weakSelf dismiss];
                } else {
                    [SVProgressHUD dismiss];
                }
            } FailedBlock:^(NSError * _Nonnull error) {
                [SVProgressHUD dismiss];
            }];
        }
            break;
            
        default:
            break;
    }
}
- (void)clickTimeButton:sender
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self.timePickerView show];
}
#pragma mark - 处理键盘
- (void)keyboardWillChangeFrame:(NSNotification *)noti
{
    CGRect keyBoardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]floatValue];
    //0.250000
    CGFloat top = keyBoardFrame.origin.y == HEIGHT ? (HEIGHT - self.height) * 0.5 : (HEIGHT - self.height) * 0.5 - 100*ADAPTER_WIDTH;
    BOOL keyboardJump = keyBoardFrame.origin.y == HEIGHT ? NO : YES ;
    [UIView animateWithDuration:duration animations:^{
        self.top = top;
    } completion:^(BOOL finished) {
        if (keyboardJump) {
            self.popView.isClickBGDismiss = NO;
        } else {
            self.popView.isClickBGDismiss = YES;
        }
    }];
}
#pragma mark - YYTextView
- (void)textViewDidChange:(YYTextView *)textView
{
    if (textView.text.length > self.maxNumer) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 30)];
    }
    self.numberLab.text = [NSString stringWithFormat:@"%ld/%ld",textView.text.length,self.maxNumer];
}
#pragma mark - lazy懒加载
- (UILabel *)titleLab
{
    if (!_titleLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightBold];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentCenter;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(10*ADAPTER_WIDTH, 22*ADAPTER_WIDTH, self.width - 20*ADAPTER_WIDTH, 17*ADAPTER_WIDTH);
        _titleLab = lab;
    }
    return _titleLab;
}
- (UIButton *)leftButton
{
    if (!_leftButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setTitle:@"取消" forState:UIControlStateNormal];
        [button setTitleColor:rgba(254, 80, 222, 1) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightRegular];
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(24*ADAPTER_WIDTH, self.height - 58*ADAPTER_WIDTH, 120*ADAPTER_WIDTH, 38*ADAPTER_WIDTH);
        button.layer.cornerRadius = 19*ADAPTER_WIDTH;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = rgba(254, 80, 222, 1).CGColor;
        button.layer.borderWidth = 1;
        button.tag = 0;
        _leftButton = button;
    }
    return _leftButton;
}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:WhiteColor forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16*ADAPTER_WIDTH weight:UIFontWeightRegular];
        [button addTarget:self action:@selector(clickItemButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(17*ADAPTER_WIDTH + self.leftButton.right, self.height - 58*ADAPTER_WIDTH, 120*ADAPTER_WIDTH, 38*ADAPTER_WIDTH);
        button.layer.cornerRadius = 19*ADAPTER_WIDTH;
        button.layer.masksToBounds = YES;
        button.backgroundColor = rgba(254, 80, 222, 1);
        button.tag = 1;
        _rightButton = button;
    }
    return _rightButton;
}
- (UILabel *)timeLab
{
    if (!_timeLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(20*ADAPTER_WIDTH, self.height - 114*ADAPTER_WIDTH,50*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
        _timeLab = lab;
    }
    return _timeLab;
}
- (UIControl *)timeControl
{
    if (!_timeControl) {
        UIControl *view = [[UIControl alloc]initWithFrame:CGRectMake(64*ADAPTER_WIDTH, self.height - 120*ADAPTER_WIDTH, 220*ADAPTER_WIDTH, 30*ADAPTER_WIDTH)];
        [self addSubview:view];
        view.backgroundColor = rgba(245, 245, 245, 1);
        view.layer.cornerRadius = 4*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        [view addTarget:self action:@selector(clickTimeButton:) forControlEvents:UIControlEventTouchUpInside];
        _timeControl = view;
    }
    return _timeControl;
}
- (UILabel *)timeContentLab
{
    if (!_timeContentLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.timeControl addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(10*ADAPTER_WIDTH, 0, self.timeControl.width - 30*ADAPTER_WIDTH, self.timeControl.height);
        _timeContentLab = lab;
    }
    return _timeContentLab;
}
- (UIImageView *)timeIV
{
    if (!_timeIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self.timeControl addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"MarrayPlan_arrow"];
        iv.frame = CGRectMake(self.timeControl.width - 24*ADAPTER_WIDTH, 8*ADAPTER_WIDTH, 8*ADAPTER_WIDTH, 12*ADAPTER_WIDTH);
        _timeIV = iv;
    }
    return _timeIV;
}
- (UILabel *)desLab
{
    if (!_desLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self addSubview:lab];
        lab.font = [UIFont systemFontOfSize:14*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = kColor51;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(20*ADAPTER_WIDTH, 65*ADAPTER_WIDTH,50*ADAPTER_WIDTH, 20*ADAPTER_WIDTH);
        _desLab = lab;
    }
    return _desLab;
}
- (YYTextView *)textView
{
    if (!_textView) {
        YYTextView *view = [[YYTextView alloc]initWithFrame:CGRectMake(64*ADAPTER_WIDTH, 59*ADAPTER_WIDTH, 220*ADAPTER_WIDTH, 90*ADAPTER_WIDTH)];
        [self addSubview:view];
        view.backgroundColor = rgba(245, 245, 245, 1);
        view.layer.cornerRadius = 5*ADAPTER_WIDTH;
        view.layer.masksToBounds = YES;
        view.tintColor = rgba(254, 77, 223, 1);
        view.placeholderFont = kFontRegular(14*ADAPTER_WIDTH);
        view.placeholderTextColor = rgba(187, 187, 187, 1);
        view.placeholderText = @"请输入任务描述";
        view.font = kFontRegular(14*ADAPTER_WIDTH);
        view.textColor = kColor51;
        [view setValue:@(8*ADAPTER_WIDTH) forKey:@"insetTop_"];
        [view setValue:@(28*ADAPTER_WIDTH) forKey:@"insetBottom_"];
        [view setValue:@(8*ADAPTER_WIDTH) forKey:@"insetLeft_"];
        [view setValue:@(8*ADAPTER_WIDTH) forKey:@"insetLeft_"];
        view.delegate = self;
        _textView = view;
    }
    return _textView;
}
- (BRDatePickerView *)timePickerView
{
    if (!_timePickerView) {
        BRDatePickerView *view = [[BRDatePickerView alloc]init];
        // 2.设置属性
        view.pickerMode = BRDatePickerModeYMD;
        view.title = @"选择年月日";
        view.minDate = [Utilities getDateWithTimestamp:[Utilities getCurrentTimestamp]];
        view.maxDate = [NSDate br_setYear:2050 month:12 day:1];
        view.isAutoSelect = NO;
        view.showToday = YES;
        WK(weakSelf)
        [view setResultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
            weakSelf.timeContentLab.text = selectValue;
        }];
        _timePickerView = view;
    }
    return _timePickerView;
}
- (UILabel *)numberLab
{
    if (!_numberLab) {
        UILabel *lab = [[UILabel alloc]init];
        [self.textView addSubview:lab];
        lab.font = [UIFont systemFontOfSize:12*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = rgba(187, 187, 187, 1);
        lab.textAlignment = NSTextAlignmentRight;
        lab.numberOfLines = 1;
        lab.frame = CGRectMake(10*ADAPTER_WIDTH, self.textView.height - 19*ADAPTER_WIDTH, self.textView.width - 16*ADAPTER_WIDTH, 13*ADAPTER_WIDTH);
        _numberLab = lab;
    }
    return _numberLab;
}
@end
